package controllers

import (
	"fly/global"
	"fly/helper"
	"fly/models"
	"fly/req"
	"fly/resp"
	"fly/service"
	"fmt"
	"github.com/duke-git/lancet/v2/convertor"
	"github.com/duke-git/lancet/v2/slice"
)

// SysDeptController 系统部门
type SysDeptController struct {
	BaseController
	SysDeptService  service.SysDeptService
	SysUserService  service.SysUserService
	AircraftService service.AircraftService
}

// AddSysDept ...
// @Title 添加部门
// @Description 添加一个系统配置-部门
// @Param token header  string	false "授权Token"
// @Param body body req.AddDeptReq true "添加部门数据"
// @Success 200 {object} resp.SysDeptResp
// @router /save [post]
func (c *SysDeptController) AddSysDept() {
	con := req.AddDeptReq{}
	c.CheckReqErrorAbort(c.BindJSON(&con))
	c.ValidReqParams(&con)
	c.CheckReqErrorAbort(helper.RequireTrue(con.Id == nil || *con.Id != con.Pid, "上级部门不能为自身"))

	mSysDept := models.SysDept{}
	c.CheckRespErrorAbort(helper.CopyDeep(&mSysDept, con))
	mSysDept.Pids = c.SysDeptService.GetPidList(con.Pid)
	id := int64(0)
	if con.Id != nil {
		id = *con.Id
	}
	var err error
	if id > 0 {
		subDeptList := c.SysDeptService.GetSubDeptList(id)
		c.CheckReqErrorAbort(helper.RequireTrue(!slice.Contain(subDeptList, con.Pid), "上级部门不能为下级部门"))

		old, _ := models.GetSysDeptById(id)
		c.CheckReqErrorAbort(helper.RequireTrue(old != nil, "未找到资源"))
		_ = helper.CopyOut(old, con)
		old.Pids = mSysDept.Pids
		err = models.UpdateSysDeptById(old, c.GetReqUserId())
	} else {
		id, err = models.AddSysDept(&mSysDept, c.GetReqUserId())
	}
	c.CheckRespErrorAbort(err)
	_ = global.DelCache(fmt.Sprintf(service.DeptCacheSimple, id))
	_ = global.DelCache(fmt.Sprintf(service.DeptCacheName, id))
	r := resp.SysDeptResp{}
	data, err := models.GetSysDeptById(id)
	_ = resp.Copy2TimeResp(&r, data)
	c.Success(r)
}

// All ...
// @Title 查询部门
// @Description 查询所有部门列表
// @Param token header  string	false "授权Token"
// @Success 200 {object} []resp.SysDeptResp
// @router /list [get]
func (c *SysDeptController) All() {
	subIds, err := c.SysDeptService.GetSubDeptIdListByUserId(c.GetReqUserId())
	c.CheckRespErrorAbort(err)
	deptList := c.SysDeptService.FindAllIds(subIds)
	var results []resp.SysDeptResp
	c.CheckRespErrorAbort(resp.Copy2TimeResp(&results, &deptList))
	c.Success(resp.DeptList2Tree(results))
}

// Del ...
// @Title 删除部门
// @Description 删除部门
// @Param token header  string	false "授权Token"
// @Param path id int true "部门id"
// @Success 200 {int} "id"
// @router /:id [delete]
func (c *SysDeptController) Del() {
	id, err := convertor.ToInt(c.Ctx.Input.Param(":id"))
	c.CheckReqErrorAbort(err)
	c.CheckReqErrorAbort(helper.RequireTrue(c.SysUserService.IsSuperAdminUser(c.GetReqUserId()), "超级管理员才能操作"))
	subDeptList := c.SysDeptService.GetSubDeptList(id)
	c.CheckReqErrorAbort(helper.RequireTrue(len(subDeptList) == 0, "子部门不为空"))
	userIds := c.SysUserService.GetDeptUserIds(id)
	c.CheckReqErrorAbort(helper.RequireTrue(len(userIds) == 0, "部门下面存在有效用户"))
	aircraftIds := c.AircraftService.GetIdsByDeptId(id)
	c.CheckReqErrorAbort(helper.RequireTrue(len(aircraftIds) == 0, "部门下面存在飞机"))
	c.CheckRespErrorAbort(models.DeleteSysDept(id))
	_ = global.DelCache(fmt.Sprintf(service.DeptCacheSimple, id))
	_ = global.DelCache(fmt.Sprintf(service.DeptCacheName, id))
	c.Success(id)
}

// Destroy ...
// @Title 销毁
// @Description 相当于逻辑删除部门
// @Param token header  string	false "授权Token"
// @Param path id int true "部门id"
// @Success 200 {int} "id"
// @router /destroy/:id [put]
func (c *SysDeptController) Destroy() {
	id, err := convertor.ToInt(c.Ctx.Input.Param(":id"))
	c.CheckReqErrorAbort(err)
	subDeptList := c.SysDeptService.GetSubDeptList(id)
	c.CheckReqErrorAbort(helper.RequireTrue(len(subDeptList) == 0, "子部门不为空"))
	userIds := c.SysUserService.GetDeptUserIds(id)
	c.CheckReqErrorAbort(helper.RequireTrue(len(userIds) == 0, "部门下面是存在有效用户"))
	c.CheckRespErrorAbort(models.DestroySysDept(id, c.GetReqUserId()))
	_ = global.DelCache(fmt.Sprintf(service.DeptCacheSimple, id))
	_ = global.DelCache(fmt.Sprintf(service.DeptCacheName, id))
	c.Success(id)
}
